package com.qf.service.impl;

import com.qf.constant.StrategyConstants;
import com.qf.dfa.SensitiveWordFilter;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.service.FilerChainService;
import com.qf.util.PushMsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * projectName: sms-platform02
 *
 * @author: 元
 * time: 2021/10/18 0018 16:54
 * description:敏感词策略实现类
 * 进行敏感词过滤,如果短信内容中存在某些非法内容,则不允许发送
 */
@Slf4j
@Service(value = "dirtyWords")
public class DirtyWordServiceImpl implements FilerChainService {

    @Autowired
    private PushMsgUtil pushMsgUtil;

    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        log.info("[策略模块-敏感词过滤],开始执行",standardSubmit);
        //使用DFA算法的方式
        //1.获取敏感词的过滤器对象
        SensitiveWordFilter sensitiveWordFilter = SensitiveWordFilter.getInstance();
        //2.查看敏感词信息
        int words = sensitiveWordFilter.checkSensitiveWord(standardSubmit.getMessageContent(),0,SensitiveWordFilter.minMatchType);

        //3.判断本次短信发送内容是否存在敏感词
        if (words == 0){
            log.info("[策略模块-敏感词过滤],敏感词策略开始执行,words={}",standardSubmit);
            return true;
        }else {
            log.info("[策略模块-敏感词过滤],敏感词策略已执行! 本次短信发送内容存在敏感词汇,words={}",standardSubmit,words);
            //4.将日志数据发送到日志Topic队列,让ES保存当前数据
            pushMsgUtil.sendLog(standardSubmit, StrategyConstants.STRATEGY_ERROR_DIRTY_WORDS);
            //5.将StandardReport对象封装,发送到状态报告到Topic队列中,接口模块再给用户推送
            pushMsgUtil.pushReport(standardSubmit,StrategyConstants.STRATEGY_ERROR_DIRTY_WORDS);
            log.info("[策略模块-限流策略],敏感词策略执行成功！！！sendTime={},fiveBefore={}, sendNumbers={}",standardSubmit);
            return false;
        }

    }

}
